package com.wannengbang.storemobile.homepage;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.github.gcacace.signaturepad.views.SignaturePad;
import com.gyf.immersionbar.ImmersionBar;
import com.wannengbang.storemobile.R;
import com.wannengbang.storemobile.base.DataCallBack;
import com.wannengbang.storemobile.bean.ImageBean;
import com.wannengbang.storemobile.event.SignBitmapEvent;
import com.wannengbang.storemobile.homepage.model.HomePageModelImpl;
import com.wannengbang.storemobile.utils.ActivityUtil;
import com.wannengbang.storemobile.utils.Base64BitmapUtil;
import com.wannengbang.storemobile.utils.DateTimeUtil;
import com.wannengbang.storemobile.widget.ViewLoading;

import org.greenrobot.eventbus.EventBus;

import java.util.Date;

public class PenActivity extends AppCompatActivity {

    SignaturePad mSignaturePad;
    private String currentTime;
    private View viewProtocol2;
    private Bitmap bitmapProtocol2;
    private Bitmap signBitmap;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pen);
        ActivityUtil.getInstance().addActivity(this); // 添加到Activity工具类
        type = getIntent().getStringExtra("type");
        currentTime = DateTimeUtil.format(new Date());
        ImmersionBar.with(this)
                .statusBarDarkFont(false)
                .navigationBarDarkIcon(false)
                .navigationBarColor(R.color.white)
                .statusBarDarkFont(false, 0.2f)
                .init();
        mSignaturePad = (SignaturePad) findViewById(R.id.signature_pad);
        mSignaturePad.setOnSignedListener(new SignaturePad.OnSignedListener() {

            @Override
            public void onStartSigning() {
                //Event triggered when the pad is touched
            }

            @Override
            public void onSigned() {
                //Event triggered when the pad is signed
            }

            @Override
            public void onClear() {
                //Event triggered when the pad is cleared
            }
        });

        //取消返回
        Button buttonBack = (Button) findViewById(R.id.buttonBack);
        buttonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });

        //清除签名
        Button buttonClear = (Button) findViewById(R.id.buttonClear);
        buttonClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                mSignaturePad.clearView();
            }
        });

        //完成签名
        Button buttonOk = (Button) findViewById(R.id.buttonOk);
        buttonOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                signBitmap = mSignaturePad.getSignatureBitmap();
                ViewLoading.showProgress(PenActivity.this, "上传中...");
                String file = Base64BitmapUtil.bitmapToBase64(signBitmap, 30);
                new HomePageModelImpl().requestUploadFile("store", file, new DataCallBack<ImageBean>() {
                    @Override
                    public void onSuccessful(ImageBean bean) {
                        EventBus.getDefault().post(new SignBitmapEvent(bean.getData().getUrl()));
                        finish();
                    }

                    @Override
                    public void onFailed(String msg, String code) {

                    }
                });
            }
        });
    }


    private Bitmap loadBitmapFromView(View v) {
        int w = v.getWidth();
        int h = v.getHeight();

        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmp);

//        c.drawColor(Color.WHITE);
        /** 如果不设置canvas画布为白色，则生成透明 */

        v.layout(0, 0, w, h);
        v.draw(c);

        return bmp;
    }


    private void layoutView(View v, int width, int height) {
        // 指定整个View的大小 参数是左上角 和右下角的坐标
        v.layout(0, 0, width, height);
        int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
        int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.AT_MOST);
        /** 当然，measure完后，并不会实际改变View的尺寸，需要调用View.layout方法去进行布局。
         * 按示例调用layout函数后，View的大小将会变成你想要设置成的大小。
         */
        v.measure(measuredWidth, measuredHeight);
        v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    }

}